package org.budo.weixin.platform.util;

import org.budo.support.lang.util.NumberUtil;
import org.budo.support.lang.util.StringUtil;
import org.budo.support.slf4j.Slf4j;
import org.budo.weixin.platform.AuthorizeMessage;
import org.budo.weixin.platform.Message;
import org.budo.weixin.platform.TicketMessage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;

/**
 * @author ZRP
 * @version 1 (2016年9月22日下午10:44:25)
 * @since Java7
 */
public class WeixinThirdPlatformUtil {
    private static final Logger log = Slf4j.getLogger();

    public static Message stringToMessage(String requestBody) {
        try {
            Message message = null;
            Document document = Jsoup.parse(requestBody);
            Elements xml = document.select("xml");
            if (null == xml || xml.isEmpty()) {
                log.error("错误的微信消息, xml为空, requestBody={}", requestBody);
                return null;
            }
            String infoType = xml.select("InfoType").text();
            if (StringUtil.isEmpty(infoType)) {
                if (StringUtil.isEmpty(xml.select("Encrypt").text())) {
                    log.error("错误的微信消息, MsgType为空, requestBody={}", requestBody);
                } else {
                    log.warn("加密微信消息, 暂不支持, requestBody={}", requestBody);
                }
                return null;
            }

            if (Message.INFO_TYPE_TICKET.equalsIgnoreCase(infoType)) {
                message = new TicketMessage();
                ((TicketMessage) message).setComponentVerifyTicket(xml.select("ComponentVerifyTicket").text());
            } else {
                if (Message.INFO_TYPE_UNAUTHORIZED.equalsIgnoreCase(infoType)) {
                    message = new AuthorizeMessage();
                    ((AuthorizeMessage) message).setAuthorizerAppid(xml.select("AuthorizerAppid").text());
                } else if (Message.INFO_TYPE_UPDATEAUTHORIZED.equalsIgnoreCase(infoType) || Message.INFO_TYPE_AUTHORIZED.equalsIgnoreCase(infoType)) {
                    message = new AuthorizeMessage();
                    ((AuthorizeMessage) message).setAuthorizerAppid(xml.select("AuthorizerAppid").text());
                    ((AuthorizeMessage) message).setAuthorizationCode(xml.select("AuthorizationCode").text());
                    String authorizationCodeExpiredTime = xml.select("AuthorizationCodeExpiredTime").text();
                    ((AuthorizeMessage) message).setAuthorizationCodeExpiredTime(NumberUtil.toInteger(authorizationCodeExpiredTime));
                } else {
                    log.error("未处理的消息类型 " + requestBody);
                }

            }

            if (null != message) {
                message.setAppId(xml.select("AppId").text());
                message.setCreateTime(NumberUtil.toLong(xml.select("CreateTime").text()));
                message.setInfoType(xml.select("InfoType").text());
            }
            return message;
        } catch (Exception e) {
            log.error("解析消息出错 \n" + requestBody + "\n", e);
            return null;
        }
    }
}
